package view;

import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import logic.FileSystem;
import logic.MyFile;
import view.controller.WriteWindowController;


/**
 * Created with IntelliJ IDEA.
 *
 * @Author: SmartBoy
 * @Date: 2021/06/02/16:07
 * @Description:
 */
public class WriteWindow {
    public static void display(Parent root, WriteWindowController controller, MyFile file) {
        controller.setFile(file);
        FileSystem fileSystem = FileSystem.getInstance();


        Stage stage = new Stage();
        Scene scene = new Scene(root, 600, 500);
        stage.setTitle(file.getName() + "." + file.getType());

        controller.filename.setText("文本内容  " + (file.getAttr()%2==1 ? "（只读）" : "") );

        String content = (String) fileSystem.openFile(file).getObj();
        controller.content.setText(content);
        controller.overwriteBtn.setOnMouseClicked(e -> {
            fileSystem.writeFile(file, controller.content.getText(), true);
            //System.out.println(fileSystem.openFile(file).getObj());
            stage.close();
        });
        controller.appendBtn.setOnMouseClicked(e -> {
            fileSystem.writeFile(file, controller.appendContent.getText(), false);
            //System.out.println(fileSystem.openFile(file).getObj());
            stage.close();
        });
        controller.cancelBtn.setOnMouseClicked(e -> {
            fileSystem.closeFile(file);
            stage.close();
        });

        stage.setOnCloseRequest(e -> {
            fileSystem.closeFile(file);
            //System.out.println(file.getName() + "." + file.getType() + " 已关闭！");
        });

        if(file.getAttr()%2 == 1) {
            controller.overwriteBtn.setDisable(true);
            controller.appendBtn.setDisable(true);
            controller.content.setEditable(false);
            controller.appendContent.setDisable(true);
        }

        stage.setScene(scene);
        stage.showAndWait();
    }

}
